package br.ufrj.dcc.tp.cliente.gui;

import java.awt.Container;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;

import javax.swing.BoxLayout;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTextArea;
import javax.swing.SwingUtilities;
import javax.swing.border.TitledBorder;

import br.ufrj.dcc.tp.cliente.controller.ClientController;
import br.ufrj.dcc.tp.cliente.controller.TextAreaController;
import br.ufrj.dcc.tp.cliente.gui.event.EventType;
import br.ufrj.dcc.tp.cliente.gui.event.MessageEvent;
import br.ufrj.dcc.tp.cliente.gui.event.RecipientEvent;

public class Window extends JFrame {
	
	private static final long serialVersionUID = 1L;
	private static final String templateTitle = "Bate-papo TP - Cliente %s";
	/** Historico das mensagens. */
	private JTextArea msgHistory;
	/** Mensagem para envio. */
	private JTextArea msgSend;
	/** Painel do historico. */
	private JScrollPane scrollPaneHistory;
	/** Painel do envio. */
	private JScrollPane scrollPaneSend;
	/** Painel de usuarios. */
	private JScrollPane scrollPaneUsers;
	/** Caixas para marcar usuarios. */
	private ArrayList<JCheckBox> usuarios;
	
	private JSplitPane splitPane;
	private JPanel panelUsers;
	private ClientController controller;
	
	public Window(ClientController controller, TextAreaController textareacontroller) {
		super(templateTitle.replace("%s", ""));
		this.controller = controller;
		
		panelUsers = new JPanel();
		JPanel panelLeft = new JPanel();
		
		GridBagConstraints gc = new GridBagConstraints();
		
		Container c = getContentPane();
		
		panelUsers.setLayout(new BoxLayout(panelUsers, BoxLayout.Y_AXIS));
		
		msgHistory = new JTextArea();
		msgSend = new JTextArea();
		msgSend.addKeyListener(textareacontroller);
	
		scrollPaneHistory = new JScrollPane(msgHistory);
		scrollPaneSend = new JScrollPane(msgSend);
		scrollPaneUsers = new JScrollPane(panelUsers);
		
		panelLeft.setLayout(new GridBagLayout());
		
		gc.fill = GridBagConstraints.BOTH;
		gc.gridx = 0;
		gc.gridy = 0;
		gc.weightx = 3;
		gc.weighty = 3;
		gc.gridwidth = 1;
		gc.gridheight = 1;
		
		scrollPaneHistory.setBorder(new TitledBorder("Output"));
		
		panelLeft.add(scrollPaneHistory, gc);
		
		gc.gridx = 0;
		gc.gridy = 1;
		gc.weightx = 3;
		gc.weighty = 1;
		gc.gridwidth = 1;
		gc.gridheight = 1;
		
		scrollPaneSend.setBorder(new TitledBorder("Input"));
		panelLeft.add(scrollPaneSend, gc);
		
		msgHistory.setEditable(false);
		msgHistory.setLineWrap(true);
		msgSend.setLineWrap(true);
		
		usuarios = new ArrayList<JCheckBox>();
		
		splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, panelLeft, scrollPaneUsers);
		splitPane.setDividerSize(5);
		splitPane.setResizeWeight(0.5);
		c.add(splitPane);
		
		//window parameters
		setSize(700, 450);
		setLocationRelativeTo(null);
		setVisible(false);
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		
	}
	
	public void setRecipients(List<String> users) {
		try {
			SwingUtilities.invokeAndWait(new RecipientEvent(users, usuarios, panelUsers, EventType.RECIPIENT));
		} catch (Exception e) {
		}
		getContentPane().repaint();
	}
	
	public void cleanUsers() {
		SwingUtilities.invokeLater(new RecipientEvent(this.usuarios, EventType.CLEAR));
	}
	
	public void cleanMessage() {
		SwingUtilities.invokeLater(new MessageEvent(this.msgSend, EventType.CLEAR));
	}
	
	public String getMessage() {
		return msgSend.getText();
	}
	
	public List<Integer> getRecipients() {
		ArrayList<Integer> recipients = new ArrayList<Integer>();
		
		for( int i = 0; i < usuarios.size(); ++i ) {
			if( usuarios.get(i).isSelected() ) {
				recipients.add(i);
			}
		}
		return recipients;
	}
	
	public void appendMessage(String message) {
		SwingUtilities.invokeLater(new MessageEvent(this.msgHistory, message.concat("\n"), EventType.APPEND));
	}
	
	@Override
	public void dispose() {
		int response = JOptionPane.showConfirmDialog(null ,"Deseja sair?", "Saída",JOptionPane.YES_NO_OPTION);
		if(response == JOptionPane.YES_OPTION) {
			controller.finish();
			silentDispose();
		}
	}
	
	public void silentDispose() {
		super.dispose();
	}
	
	@Override
	public void setTitle(String value) {
		super.setTitle(templateTitle.replace("%s", value));
	}
}
